/*-----------------------------------------------------------------------------
* @Description:     人员基本信息列表
* @Version:         1.0.0
* @author:          lily(529116421@qq.com)
* @date             2017.11.07
* ==NOTES:=============================================
* v1.0.0(2017.12.04):
     初始生成
* ---------------------------------------------------------------------------*/
$(function(){
	//页面刷初始数据
	getData();

	//点击年级弹框添加事件
	$('.J_addGradeDlg').click(function(){
		addGrade();
	});

	//点击组别弹框添加事件
	$('.J_addGroupDlg').click(function(){
		addGroup();
	});

	//删除年级
	$(document).on('click', '.J_delGrade', function(e){
		delGrade(e);
	});

	//删除组别
	$(document).on('click', '.J_delGroup', function(e){
		delGroup(e);
	});

	/**
     * 页面刷初始数据
     */
    function getData(){
    	var strGrade = '',
    		strGroup = '',
    		form = {
    			data: 1
    		};
    	$.ajax({
            type: jQuery.url.BasicInformationManagement.getInitData.type,
            url: jQuery.url.BasicInformationManagement.getInitData.url,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: form,     //JSON.stringify
            dataType: "json",
            success: function (rs) {
                $('.J_gradeList').empty();
                $('.J_groupList').empty();
                if(rs.code == 0){
                    $.each(rs.listGrade, function(index, item){
                        strGrade += '<div data-id="'+ item.id +'" class="select-tag J_grades">\
                        				'+ item.grade +'<i class="fa fa-times J_delGrade"></i>\
                        			</div>';
                    });
                    $('.J_gradeList').append(strGrade);
                    $.each(rs.listGroup, function(index, item){
                        strGroup += '<div data-id="'+ item.id +'" class="select-tag J_groups">\
                        				'+ item.group +'<i class="fa fa-times J_delGroup"></i>\
                        			</div>';
                    }); 
                    $('.J_groupList').append(strGroup);
                }else{                
                    Alert('提示信息', '操作失败，返回参数不正确!');
                }
            },
            error: function (message) {
                Alert("提示信息", "请求发送失败。")
            }
        });
    }

    /**
     * 添加年级事件
     */
    function addGrade(){
    	var
    		grade = $('.J_grade').val(),
    		length = $('.J_grades').length,
    		grades = [],
    		ajax = 0,
    		str = '';
    	
		$('.J_grades').each(function(index, item){
			grades.push($(this).text());   			
    		if(grade == $.trim(grades[index])){
    			Alert('提示信息', '该年级已存在，请重新添加！');
    			ajax += 0;
    		}else{
    			ajax += 1;
    		}
    	});
    	if(ajax == length){
    		$.ajax({
	            type: jQuery.url.BasicInformationManagement.addGradeData.type,
	            url: jQuery.url.BasicInformationManagement.addGradeData.url,
	            contentType: "application/x-www-form-urlencoded; charset=utf-8",
	            data: {grade: grade},     //JSON.stringify
	            dataType: "json",
	            success: function (rs){
	            	if(rs.code == 0){
	            		window.location.reload();
	            	}else{                
	                    Alert('提示信息', '操作失败，返回参数不正确!');
	                }
	            },
	            error: function (message) {
	                Alert("提示信息", "请求发送失败。");
	            }
        	});
    	}
    }
    
    /**
     * 添加组别事件
     */
    function addGroup(){
    	var
    		group = $('.J_group').val(),
    		length = $('.J_groups').length,
    		groups = [],
    		ajax = 0,
    		str = '';
    	
		$('.J_groups').each(function(index, item){
			groups.push($(this).text());   			
    		if(group == $.trim(groups[index])){
    			Alert('提示信息', '该组别已存在，请重新添加！');
    			ajax += 0;
    		}else{
    			ajax += 1;
    		}
    	});
    	if(ajax == length){
    		$.ajax({
	            type: jQuery.url.BasicInformationManagement.addGroupData.type,
	            url: jQuery.url.BasicInformationManagement.addGroupData.url,
	            contentType: "application/x-www-form-urlencoded; charset=utf-8",
	            data: {group: group},     //JSON.stringify
	            dataType: "json",
	            success: function (rs){
	            	if(rs.code == 0){
	            		window.location.reload();
	            	}else{                
	                    Alert('提示信息', '操作失败，返回参数不正确!');
	                }
	            },
	            error: function (message) {
	                Alert("提示信息", "请求发送失败。");
	            }
        	});
    	}
    }

    /**
     * 删除年级事件
     */
    function delGrade(e){
    	var
    		id = $(e.target).parent('div').attr('data-id');
    	
    	$.ajax({
            type: jQuery.url.BasicInformationManagement.delGradeData.type,
            url: jQuery.url.BasicInformationManagement.delGradeData.url,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: {gradeId: id},//JSON.stringify
            dataType: "json",
            success: function (rs){
            	if(rs.code == 0){
            		LiterTip("提示信息", "删除成功！");
                    setTimeout(function(){
                        window.location.reload();
                    }, 1000);
            	}else{
                    Alert('提示信息', rs.errMsg);
                }
            },
            error: function (message) {
                Alert("提示信息", "请求发送失败。");
            }
    	});
    }

    /**
     * 删除组别事件
     */
    function delGroup(e){
    	var
    		id = $(e.target).parent('div').attr('data-id');
    	
    	$.ajax({
            type: jQuery.url.BasicInformationManagement.delGroupData.type,
            url: jQuery.url.BasicInformationManagement.delGroupData.url,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: {groupId: id},//JSON.stringify
            dataType: "json",
            success: function (rs){
            	if(rs.code == 0){
            		LiterTip("提示信息", "删除成功！");
                    setTimeout(function(){
                        window.location.reload();
                    }, 1000);
            	}else{
                    Alert('提示信息', rs.errMsg);
                }
            },
            error: function (message) {
                Alert("提示信息", "请求发送失败。");
            }
    	});
    }
})